package 数据结构练习;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class 逆波兰 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] strs = str.split(" ");
        List<String> strings = change(strs);
        for(String c:strings){
            System.out.print(c+" ");
        }
        System.out.println();
        System.out.println(calculate(strings));
    }

    public static List<String> change(String[] str){
        Stack stack1 = new Stack();
        List<String> strs = new ArrayList<>();
        int point = 0;
        while (point<str.length){
            String c = str[point];
            try {
                int c2 = Integer.parseInt(c);
                strs.add(c);
            }catch (Exception e){
                //如果是右括号
                if(c.equals(")")) {
                    int count = stack1.find();
                    for(int i=0;i<count;i++){
                        strs.add(stack1.pop());
                    }
                    stack1.pop();
                }else {
                    if((c.equals("+") || c.equals("-")) && (stack1.get().equals("*") || stack1.get().equals("/"))){
                        while (stack1.top>0){
                            strs.add(stack1.pop());
                        }
                    }
                    stack1.push(c);
                }
                point++;
                continue;
            }
            point++;
        }
        while (stack1.top>0){
            strs.add(stack1.pop());
        }
        return strs;
    }

    public static int calculate(List<String> Strings){
        int result = 0;
        int point = 0;
        Stack stack1 = new Stack();
        while (point<Strings.size()){
            String c = Strings.get(point);
            try {
                int c2 = Integer.parseInt(c);
                stack1.push(c);
            }catch (Exception e){
                char c2 = c.charAt(0);
                switch (c2){
                    case '+':
                       stack1.push(String.valueOf(Integer.parseInt(stack1.pop()) + Integer.parseInt(stack1.pop())));
                        break;
                    case '-':
                        stack1.push(String.valueOf(Integer.parseInt(stack1.pop()) - Integer.parseInt(stack1.pop())));
                        break;
                    case '*':
                        stack1.push(String.valueOf(Integer.parseInt(stack1.pop()) * Integer.parseInt(stack1.pop())));
                        break;
                    case '/':
                        stack1.push(String.valueOf(Integer.parseInt(stack1.pop()) / Integer.parseInt(stack1.pop())));
                        break;
                }
                point++;
                continue;
            }
            point++;
        }
        return Integer.parseInt(stack1.pop());
    }

    //   5 * ( 3 + 2 ) + 3    5 3 2 + * 3 +   28
}

class Stack{
    String[] stack = new String[100];
    int top = 0;

    public void push(String x){
        stack[top] = x;
        top++;
    }

    public String pop(){
        top--;
        return stack[top];
    }

    public String get(){
        return stack[top-1];
    }

    public int find(){
        int count = 0;
        int point = top-1;
        while (!stack[point].equals("(")){
            count++;
            point--;
        }
        return count;
    }
}
